package com.august.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;


@Configuration
@Slf4j
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true)
public class DataSourceConfig {
    //主数据源配置 ds1数据源
//    @Primary
//    @Bean(name = "mysql1DataSourceProperties")
//    @ConfigurationProperties(prefix = "spring.datasource.mysql1")
//    public DataSourceProperties mysql1DataSourceProperties() {
//        return new DataSourceProperties();
//    }

    //主数据源 ds1数据源
    @Primary
    @Bean(name = "mysql1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql1")
    public DataSource ds1DataSource() {
      //  return dataSourceProperties.initializeDataSourceBuilder().build();

        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }


    //第二个ds2数据源配置
//    @Bean(name = "mysql2DataSourceProperties")
//    @ConfigurationProperties(prefix = "spring.datasource.mysql2")
//    public DataSourceProperties mysql2DataSourceProperties() {
//        return new DataSourceProperties();
//    }

    //第二个ds2数据源
    @Bean("mysql2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.mysql2")
    public DataSource mysql2DataSource() {
        //return dataSourceProperties.initializeDataSourceBuilder().build();
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }
}
